package net.zkbc.helloworld.script;

import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.stereotype.Component;

import net.zkbc.shared.util.BeanFactoryUtils;
import net.zkbc.shared.util.Cryptos;

@Component
public class DSInitScript implements BeanFactoryPostProcessor {

	private static final Logger LOG = LoggerFactory.getLogger(DSInitScript.class);

	private static final byte[] JDBC_PWD_AESKEY = Base64.getDecoder().decode("s9hT+SO8C6QrDIEj0Suyzw==");

	public static void main(String[] args) {
		System.out.println(Cryptos.aesEncrypt("zkbc", JDBC_PWD_AESKEY));
		System.out.println(Cryptos.aesEncrypt("Zkbc@2016", JDBC_PWD_AESKEY));
	}

	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		LOG.info("Initializing...");
		try {
			BeanFactoryUtils.decryptDataSourceProperties(beanFactory, JDBC_PWD_AESKEY);
		} catch (Exception e) {
			LOG.debug(e.getMessage(), e);
		}
	}
}
